/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jeevandeshmukh.glidetoastlib;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.Timer;
import java.util.TimerTask;

public class Dialog extends ToastDialog {
    private Component mDialogView;
    AnimatorProperty slideRightIn;
    AnimatorProperty slideLeftOut;
    private float animationRightInFromX = 1000;
    private float animationRightInToX = 0;
    private long animationDurationIn = 300;
    private float animationLeftOutFromX = 0;
    private float animationLeftOutToX = -1500;
    private float animationTransparency = 1;
    private long animationDurationOut = 500;

    public Dialog(Context context) {
        super(context);
    }

    /**
     * hide dialog
     */
    @Override
    public void hide() {
        if (slideLeftOut != null) {
            slideLeftOut.setStateChangedListener(
                    new Animator.StateChangedListener() {
                        @Override
                        public void onStart(Animator animator) {
                        }

                        @Override
                        public void onStop(Animator animator) {
                            Dialog.super.destroy();
                        }

                        @Override
                        public void onCancel(Animator animator) {
                        }

                        @Override
                        public void onEnd(Animator animator) {
                        }

                        @Override
                        public void onPause(Animator animator) {
                        }

                        @Override
                        public void onResume(Animator animator) {
                        }
                    });
            slideLeftOut.start();
        } else {
            super.hide();
        }
    }

    /**
     * show dialog
     */
    @Override
    public void show() {
        super.show();
        if (slideRightIn != null) {
            slideRightIn.setStateChangedListener(
                    new Animator.StateChangedListener() {
                        @Override
                        public void onStart(Animator animator) {
                        }

                        @Override
                        public void onStop(Animator animator) {
                        }

                        @Override
                        public void onCancel(Animator animator) {
                        }

                        @Override
                        public void onEnd(Animator animator) {
                        }

                        @Override
                        public void onPause(Animator animator) {
                        }

                        @Override
                        public void onResume(Animator animator) {
                        }
                    });
            slideRightIn.start();
        }
    }


    public void initAnimatorGroup(ComponentContainer componentContainer) {
        mDialogView = componentContainer.findComponentById(ResourceTable.Id_layout);
        slideRightIn = new AnimatorProperty(mDialogView);
        slideRightIn.moveFromX(animationRightInFromX).moveToX(animationRightInToX).alpha(animationTransparency).setDuration(animationDurationIn);
        slideRightIn.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);

        slideLeftOut = new AnimatorProperty(mDialogView);
        slideLeftOut.moveFromX(animationLeftOutFromX).moveToX(animationLeftOutToX).alpha(animationTransparency).setDuration(animationDurationOut);
        slideLeftOut.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);
    }
}
